package svmc.stp.hoaitt;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Sort all file in a folder by increasing with size
 * @author M.O
 *	TrinhNX 01/29: Comment code
 * 	Good point: Very good about Comparator<>, good job, my girl
 * 	Bad point: About analysis how to retrieve all file / folders / sub folders
 * 	Point 8
 * 	Training question: List a directory (which one is a file , which one is a folder.
 * 	We assume that. A file only contains data
 * 	A folder is a file that contain another files / another folders. 
 * *Note* No mention about the sub - folder :P
 */
class WaySortFile implements Comparator<File> {

	@Override
	public int compare(File file1, File file2) {
		if (file1.length() < file2.length()) {
			return -1;//
		} else {
			return 1;//
		}
	}
}

public class SortFile {

	//	private static void readFile(final String fileName)
	//			throws FileNotFoundException {
	//		FileReader fr = new FileReader(new File(fileName));
	//	}
	//
	//	private static void readFile1(final String fileName) {
	//		try {
	//			FileReader fr = new FileReader(new File(fileName));
	//		} catch (IOException e) {
	//			throw new NumberFormatException("Test");
	//		}
	//	}
	//
	//	private static void readFile3(final String fileName) {
	//		try {
	//			FileReader fr = new FileReader(new File(fileName));
	//		} catch (IOException e) {
	//			System.err.println(e);
	//		}
	//	}

	public static void main(String[] args) {
		ArrayList<File> files = new ArrayList<File>();

		File input = new File(
				"C:\\Users\\M.O\\Downloads\\Compressed\\java_course\\Day2");

		if (input.isDirectory()) {
			for (File child : input.listFiles()) {
				if (!child.isDirectory()) {
					files.add(child);
				} else {
					for (File subFile : child.listFiles()) {
						files.add(subFile);
					}
				}
			}
		}

//		Collections.sort(files, new Comparator<File>() {
//			@Override
//			public int compare(File o1, File o2) {
//				return (int) (o1.length() - o2.length());
//			}
//		});
		
		Collections.sort(files, new WaySortFile());
		for (File file : files) {
			System.out.println(file.getName() + " " + file.length());
		}
	}

	private static void printMe(WaySortFile data) {
		System.out.println(data.toString());
	}
}
